package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.pecvd_5_report.PECVD5ReportDataBean;
import com.navi.bean.out.pecvd_5_report.PECVD5ReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.IPECVD5ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;
import static com.navi.mould1.reportUtils.ComputeUtil.getPercent;
import static com.navi.mould1.reportUtils.ComputeUtil.getRate;


@Slf4j
@Service
public class PECVD5ReportServiceImpl implements IPECVD5ReportService {


    public List<PECVD5ReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("substring(A.data_value_389, 3, 3) AS zhNumber,");
        sqlVo.appendSql("substring(A.data_value_389, 7, 3) AS zhCount,");
        sqlVo.appendSql("substring(A.data_value_389, 6, 1) AS pipeNumber,");
        sqlVo.appendSql("A.data_value_338 AS filmThickness,");
        sqlVo.appendSql("A.data_value_001 AS grade,");
        sqlVo.appendSql("A.rpt_timestamp,");
        sqlVo.appendSql("A.data_value_011,");
        sqlVo.appendSql("A.data_value_020,");
        sqlVo.appendSql("A.data_value_021,");
        sqlVo.appendSql("A.data_value_022,");
        sqlVo.appendSql("A.data_value_023,");
        sqlVo.appendSql("A.data_value_025,");
        sqlVo.appendSql("A.data_value_026,");
        sqlVo.appendSql("A.data_value_037,");
        sqlVo.appendSql("A.data_value_040,");
        sqlVo.appendSql("A.data_value_074,");
        sqlVo.appendSql("A.data_value_075,");
        sqlVo.appendSql("A.data_value_076,");
        sqlVo.appendSql("A.data_value_106,");
        sqlVo.appendSql("A.data_value_220,");
        sqlVo.appendSql("A.data_value_221,");
        sqlVo.appendSql("A.data_value_222,");
        sqlVo.appendSql("A.data_value_279,");
        sqlVo.appendSql("A.data_value_280,");
        sqlVo.appendSql("A.data_value_281,");
        sqlVo.appendSql("A.data_value_319,");
        sqlVo.appendSql("A.data_value_338,");
        sqlVo.appendSql("A.data_value_343");
        sqlVo.appendSql("FROM icim_jn.spc_ret_mfg_data_pecvd A");
        sqlVo.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND A.LINE_ID_FK in" + jointInSql(conditions.getBay_id()));
        if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo.appendSql("AND A.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
        if (StringUtils.isNotEmpty(conditions.getPipe_number())) sqlVo.appendSql("AND substring(A.data_value_389, 6, 1) in" + jointInSql(conditions.getPipe_number()));
        if (StringUtils.isNotEmpty(conditions.getZh_number())) sqlVo.appendSql("AND substring(A.data_value_389, 3, 3) in" + jointInSql(conditions.getZh_number()));
        if (StringUtils.isNotEmpty(conditions.getZh_count())) sqlVo.appendSql("AND substring(A.data_value_389, 7, 3) in" + jointInSql(conditions.getZh_count()));
        if (StringUtils.isNotEmpty(conditions.getZh_place())) sqlVo.appendSql("AND substring(A.data_value_389, 10, 1) in" + jointInSql(conditions.getZh_place()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND RPT_TIMESTAMP >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND RPT_TIMESTAMP < ?", conditions.getEnd_time());
        sqlVo.appendSql("GROUP BY TOOL_ID_FK, DATA_VALUE_338, DATA_VALUE_389");
        sqlVo.appendSql("ORDER BY RPT_TIMESTAMP DESC");

        List<PECVD5ReportDataBean> dataBeanList = mySql_QueryRunner_QueryListBySql(sqlVo, PECVD5ReportDataBean.class);

        List<PECVD5ReportGridOut> girdOutList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dataBeanList)) {
            long total_count = dataBeanList.size();
            long cumulative_count = 0;
            long value_011_count = dataBeanList.stream().filter(data -> data.getData_value_011() != null && !"A".equals(data.getData_value_011())).count();
            long value_020_count = dataBeanList.stream().filter(data -> data.getData_value_020() != null && !"A".equals(data.getData_value_020())).count();
            long value_021_count = dataBeanList.stream().filter(data -> data.getData_value_021() != null && !"A".equals(data.getData_value_021())).count();
            long value_022_count = dataBeanList.stream().filter(data -> data.getData_value_022() != null && !"A".equals(data.getData_value_022())).count();
            long value_023_count = dataBeanList.stream().filter(data -> data.getData_value_023() != null && !"A".equals(data.getData_value_023())).count();
            long value_025_count = dataBeanList.stream().filter(data -> data.getData_value_025() != null && !"Normal".equals(data.getData_value_025())).count();
            long value_026_count = dataBeanList.stream().filter(data -> data.getData_value_026() != null && !"Normal".equals(data.getData_value_026())).count();
            long value_037_count = dataBeanList.stream().filter(data -> data.getData_value_037() != null && !"A".equals(data.getData_value_037())).count();
            long value_040_count = dataBeanList.stream().filter(data -> data.getData_value_040() != null && !"A".equals(data.getData_value_040())).count();
            long value_074_count = dataBeanList.stream().filter(data -> data.getData_value_074() != null && !"0.0".equals(data.getData_value_074())).count();
            long value_075_count = dataBeanList.stream().filter(data -> data.getData_value_075() != null && !"0.0".equals(data.getData_value_075())).count();
            long value_076_count = dataBeanList.stream().filter(data -> data.getData_value_076() != null && !"0.0".equals(data.getData_value_076())).count();
            long value_106_count = dataBeanList.stream().filter(data -> data.getData_value_106() != null && !"0.0".equals(data.getData_value_106())).count();
            long value_220_count = dataBeanList.stream().filter(data -> data.getData_value_220() != null && !"0.0".equals(data.getData_value_220())).count();
            long value_221_count = dataBeanList.stream().filter(data -> data.getData_value_221() != null && !"0.0".equals(data.getData_value_221())).count();
            long value_222_count = dataBeanList.stream().filter(data -> data.getData_value_222() != null && !"0.0".equals(data.getData_value_222())).count();
            long value_279_count = dataBeanList.stream().filter(data -> data.getData_value_279() != null && !"0.0".equals(data.getData_value_279())).count();
            long value_280_count = dataBeanList.stream().filter(data -> data.getData_value_280() != null && !"0.0".equals(data.getData_value_280())).count();
            long value_281_count = dataBeanList.stream().filter(data -> data.getData_value_281() != null && !"0.0".equals(data.getData_value_281())).count();
            long value_319_count = dataBeanList.stream().filter(data -> data.getData_value_319() != null && !"0.0".equals(data.getData_value_319())).count();
            long value_338_count = dataBeanList.stream().filter(data -> data.getData_value_338() != null && !"0.0".equals(data.getData_value_338())).count();
            long value_343_count = dataBeanList.stream().filter(data -> data.getData_value_343() != null && !"0.0".equals(data.getData_value_343())).count();
            if (value_011_count > 0) cumulative_count = calculatePercentage(value_011_count, total_count, cumulative_count, girdOutList, "崩边缺角");
            if (value_020_count > 0) cumulative_count = calculatePercentage(value_020_count, total_count, cumulative_count, girdOutList, "硅片表面质量差，如有孔洞");
            if (value_021_count > 0) cumulative_count = calculatePercentage(value_021_count, total_count, cumulative_count, girdOutList, "划伤");
            if (value_022_count > 0) cumulative_count = calculatePercentage(value_022_count, total_count, cumulative_count, girdOutList, "未分类的");
            if (value_023_count > 0) cumulative_count = calculatePercentage(value_023_count, total_count, cumulative_count, girdOutList, "小白点");
            if (value_025_count > 0) cumulative_count = calculatePercentage(value_025_count, total_count, cumulative_count, girdOutList, "内部膜厚");
            if (value_026_count > 0) cumulative_count = calculatePercentage(value_026_count, total_count, cumulative_count, girdOutList, "边缘膜厚");
            if (value_037_count > 0) cumulative_count = calculatePercentage(value_037_count, total_count, cumulative_count, girdOutList, "暗斑/鸡蛋");
            if (value_040_count > 0) cumulative_count = calculatePercentage(value_040_count, total_count, cumulative_count, girdOutList, "过刻");
            if (value_074_count > 0) cumulative_count = calculatePercentage(value_074_count, total_count, cumulative_count, girdOutList, "脱晶缺角计数");
            if (value_075_count > 0) cumulative_count = calculatePercentage(value_075_count, total_count, cumulative_count, girdOutList, "脱晶缺角最大深度");
            if (value_076_count > 0) cumulative_count = calculatePercentage(value_076_count, total_count, cumulative_count, girdOutList, "脱晶缺角最大宽度");
            //if (value_106_count > 0) cumulative_count = calculatePercentage(value_106_count, total_count, cumulative_count, girdOutList, "边缘色彩平均值（黄边）");
            if (value_220_count > 0) cumulative_count = calculatePercentage(value_220_count, total_count, cumulative_count, girdOutList, "小点计数");
            if (value_221_count > 0) cumulative_count = calculatePercentage(value_221_count, total_count, cumulative_count, girdOutList, "小点平均面积");
            if (value_222_count > 0) cumulative_count = calculatePercentage(value_222_count, total_count, cumulative_count, girdOutList, "小点最大面积");
            if (value_279_count > 0) cumulative_count = calculatePercentage(value_279_count, total_count, cumulative_count, girdOutList, "手指印计数");
            if (value_280_count > 0) cumulative_count = calculatePercentage(value_280_count, total_count, cumulative_count, girdOutList, "手指印平均面积");
            if (value_281_count > 0) cumulative_count = calculatePercentage(value_281_count, total_count, cumulative_count, girdOutList, "手指印最大面积");
            if (value_319_count > 0) cumulative_count = calculatePercentage(value_319_count, total_count, cumulative_count, girdOutList, "脱晶计数");
            //if (value_338_count > 0) cumulative_count = calculatePercentage(value_338_count, total_count, cumulative_count, girdOutList, "表面中心膜厚平均值");
            //if (value_343_count > 0) cumulative_count = calculatePercentage(value_343_count, total_count, cumulative_count, girdOutList, "表面边缘膜厚中心值");
        }
        return girdOutList;

    }





    private long calculatePercentage(long count, long total_count, long cumulative_count, List<PECVD5ReportGridOut> girdOutList, String bad_desc) {
        cumulative_count += count;
        PECVD5ReportGridOut gridOut = new PECVD5ReportGridOut();
        gridOut.setBad_desc(bad_desc);
        gridOut.setBad_count(String.valueOf(count));
        gridOut.setBad_rate(getPercent(getRate(count, total_count)).replace("%", ""));
        gridOut.setCumulative_bad_rate(getPercent(getRate(cumulative_count, total_count)).replace("%", ""));
        gridOut.setBad_percent(getPercent(getRate(count, total_count)));
        gridOut.setCumulative_bad_percent(getPercent(getRate(cumulative_count, total_count)));
        girdOutList.add(gridOut);
        return cumulative_count;
    }


}
